/*------------------------------------------------------------------------------
#
#	Apple Macintosh Developer Technical Support
#
#	Exception handling for MPW Pascal, MacApp and MPW C
#
#	UFailure (aka Signals) - ÒExceptional code, with a few exceptions.Ó
#
#	TestCignal.c	-	Test tool for c access to enhanced UFailure
#
#	Copyright © 1985-1988 Apple Computer, Inc.
#	All rights reserved.
#
#	Versions:	1.0					11/88
#
#	Components:	UFailure.p			November 1, 1988
#				UFailure.h			November 1, 1988
#				UFailure.inc1.p		November 1, 1988
#				UFailure.a			November 1, 1988
#				TestCignal.c		November 1, 1988
#				TestCignal.make		November 1, 1988
#				TestSignal.p		November 1, 1988
#				TestSignal.make		November 1, 1988
#
#	UFailure (or Signals) is a set of exception handling routines suitable for
#	use with MacApp, MPW C, and MPW Pascal. It is a jazzed-up version of the MacApp
#	UFailure unit. There is a set of C interfaces to it as well.
#
------------------------------------------------------------------------------*/

#include	<Types.h>
#include	<stdio.h>
#include	<Errors.h>
#include	<UFailure.h>			/* c interfaces to UFailure stuff */


FailInfo	fi;


pascal void Handler(short code, long message)
{
	fprintf(stdout, "Handler() from Never(); message= %d, code= %d\n", message, code);
	/* this will do an implicit Failure() when it exits */
}


void Never()
{
	short		code;
	
	CatchCFailures(&fi, Handler);	/* any caught by this one go to Handler */

	if (code = CatchSignal()) {		/* if weÕre getting a signal thenÉ */
		fprintf(stdout, "Never() shouldnÕt get here; code= %d\n", code);
		return;
	}
	
	FreeSignal();					/* ÒpopÓ the last Catchxxx */
	
	SignalMessage(7, 77777);
}


void Failer()
{
	if (!CatchSignal())
		Never();					/* call Never; if a signal comes, fall through */

	Failure(69, 0);					/* fail no matter what */
}


long Value()
{
	short		code;
	
	if (code = CatchSignal()) {
		fprintf(stdout, "ShouldnÕt be here in Value(); code= %d\n", code);
		return code;
	}

	/* when this does its return the CatchSignal above will be automatically popped */
	if (code = CatchSignal())
		return code;
	
	Failer();
}


main()
{
	short				code;
	volatile long		registerLong = 0;
	
	InitUFailure();

	if (code = CatchSignal()) {
		fprintf(stdout, "Signal caught in main(); code = %d, registerLong = %d\n",
			code, registerLong);
		return 0;
	}
	
	registerLong = 0xffff;
	
	Signal(Value());
}